/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.alias.deals;

import com.alias.products.BootsProduct;
import com.alias.products.FotoProduct;

import java.util.*;

/**
 * @author DioDread
 */
public class Deal {

    private Date date;
    private Party buyer;
    private Party seller;
    private Map<FotoProduct, Integer> fotoProducts = new HashMap<FotoProduct, Integer>();
    private Map<BootsProduct, Integer> bootsProducts = new HashMap<BootsProduct, Integer>();
    private int totalDiscount = 0;
    static final int DEF_DISCOUNT = 10;

    public Deal(Party seller, Party buyer, Map<FotoProduct, Integer> fProducts, Map<BootsProduct, Integer> bProduct) {
        date = new Date();
        this.seller = seller;
        this.buyer = buyer;
        setProducts(fProducts, bProduct);
    }

    private void setProducts(Map<FotoProduct, Integer> fmap, Map<BootsProduct, Integer> bmap) {
        if (fmap == null) {
            this.bootsProducts = bmap;
        } else {
            this.fotoProducts = fmap;
        }
    }

    public double getSum() {
        return getCost();
    }

    /**
     * @return the date
     */
    public Date getDate() {
        return date;
    }

    /**
     * @return the buyer
     */
    public String getBuyer() {
        return buyer.getName();
    }

    public Party getBuyerObj() {
        return buyer;
    }

    /**
     * @return the seller
     */
    public String getSeller() {
        return seller.getName();
    }

    public Party getSellerObj() {
        return seller;
    }

    public Map<FotoProduct, Integer> getFotoProducts() {
        return fotoProducts;
    }

    public Map<BootsProduct, Integer> getBootsProducts() {
        return bootsProducts;
    }

    private double getFotoCost() {
        double totalPrice = 0;
        int totalQuantity = 0;
        Set<Map.Entry<FotoProduct, Integer>> fotoProducts = getFotoProducts().entrySet();
        for (Map.Entry<FotoProduct, Integer> entry : fotoProducts) {
            if (entry.getValue() > 10 && !(entry.getKey().isDigital())) {
                totalDiscount = DEF_DISCOUNT + DEF_DISCOUNT;
            } else {
                if (entry.getValue() > 10 || !(entry.getKey().isDigital())) {
                    totalDiscount = DEF_DISCOUNT;
                }
            }
            totalPrice += entry.getKey().getPrice();
            totalQuantity += entry.getValue();
            totalPrice += (totalPrice - totalPrice * (totalDiscount / 100)) * totalQuantity;
        }
        return totalPrice;
    }

    private double getBootsCost() {
        double totalPrice = 0;
        int totalQuantity = 0;
        Set<Map.Entry<BootsProduct, Integer>> bootsProducts = getBootsProducts().entrySet();
        for (Map.Entry<BootsProduct, Integer> entry : bootsProducts) {
            if (entry.getValue() > 10) {
                totalDiscount = DEF_DISCOUNT;
            }
            totalPrice += entry.getKey().getPrice();
            totalQuantity += entry.getValue();
            totalPrice += (totalPrice - totalPrice * (totalDiscount / 100)) * totalQuantity;
        }
        return totalPrice * totalQuantity;
    }

    public double getCost() {
        return getFotoCost() + getBootsCost();
    }
}
